package com.sgb.kjwl.view.widget;

import android.content.Context;
import android.view.View;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;

import com.sgb.kjwl.R;
import com.swgk.core.util.PickSelectEntity;
import com.sgb.kjwl.model.entity.packers.PickResultEntity;
import com.sgb.link.view.adapter.PickAdapter;
import com.swgk.core.customview.MaxRecyclerView;
import com.swgk.core.dialog.BasePopWindow;
import com.swgk.core.dialog.PopViewHolder;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;

import java.util.List;

public class PopularWorkersFilterPopWindow extends BasePopWindow {

    private FilterResultClick filterResultClick;

    private TextView okView;
    private TextView reSetView;
    private MaxRecyclerView rvMajor;
    private MaxRecyclerView rvTeamSize;
    private MaxRecyclerView rvArea;

    //工程专项 adapter
    private PickAdapter majorAdapter;
    //团队规模 adapter
    private PickAdapter teamSizeAdapter;
    //所在区域 adapter
    private PickAdapter areaAdapter;

    private PickResultEntity pickResultMajor;
    private PickResultEntity pickResultTeamSize;
    private PickResultEntity pickResultArea;

    public PopularWorkersFilterPopWindow(Context context) {
        super(context, DIR_RIGHT);
    }

    @Override
    protected int popLayout() {
        return R.layout.popular_workers_popwindow;
    }

    @Override
    protected void initView(PopViewHolder holder) {
        okView = holder.getPop().findViewById(R.id.ok_view);
        reSetView = holder.getPop().findViewById(R.id.reset_view);

        //工程专项rv
        rvMajor = holder.getPop().findViewById(R.id.rv_major);
        //团队规模rv
        rvTeamSize = holder.getPop().findViewById(R.id.rv_team_size);
        //所在区域rv
        rvArea = holder.getPop().findViewById(R.id.rv_area);

        okView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                filterResultClick.result(pickResultMajor, pickResultTeamSize, pickResultArea);
                dissMiss();
            }
        });

        reSetView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                majorAdapter.resetCheckDatas();
                teamSizeAdapter.resetCheckDatas();
                areaAdapter.resetCheckDatas();

                majorAdapter.setDefItem(-1);
                teamSizeAdapter.setDefItem(-1);
                areaAdapter.setDefItem(-1);
            }
        });

        majorAdapter = new PickAdapter(mContext, null, R.layout.item_pick_filter_child);
        //pickAdapterCity.setMultiSelect(true);
        majorAdapter.setItemSelect(true);
        majorAdapter.setDefItem(-1);
        rvMajor.setLayoutManager(new GridLayoutManager(mContext, 3));
        rvMajor.setAdapter(majorAdapter);
        majorAdapter.setOnItemClick(new AdapterOnItemClick<PickSelectEntity>() {
            @Override
            public void onItemClick(PickSelectEntity pickSelectEntity, int position) {
                pickResultMajor.setNo(pickSelectEntity.getNo());
                pickResultMajor.setName(pickSelectEntity.getProjectscale());
                pickResultMajor.setPosition(position);
            }
        });

        teamSizeAdapter = new PickAdapter(mContext, null, R.layout.item_pick_filter_child);
        //pickAdapterCity.setMultiSelect(true);
        teamSizeAdapter.setItemSelect(true);
        teamSizeAdapter.setDefItem(-1);
        rvTeamSize.setLayoutManager(new GridLayoutManager(mContext, 3));
        rvTeamSize.setAdapter(teamSizeAdapter);
        teamSizeAdapter.setOnItemClick(new AdapterOnItemClick<PickSelectEntity>() {
            @Override
            public void onItemClick(PickSelectEntity pickSelectEntity, int position) {
                pickResultTeamSize.setNo(pickSelectEntity.getNo());
                pickResultTeamSize.setName(pickSelectEntity.getProjectscale());
                pickResultTeamSize.setPosition(position);
            }
        });

        areaAdapter = new PickAdapter(mContext, null, R.layout.item_pick_filter_child);
        //pickAdapterCity.setMultiSelect(true);
        areaAdapter.setItemSelect(true);
        areaAdapter.setDefItem(-1);
        rvArea.setLayoutManager(new GridLayoutManager(mContext, 3));
        rvArea.setAdapter(areaAdapter);
        areaAdapter.setOnItemClick(new AdapterOnItemClick<PickSelectEntity>() {
            @Override
            public void onItemClick(PickSelectEntity pickSelectEntity, int position) {
                pickResultArea.setNo(pickSelectEntity.getNo());
                pickResultArea.setName(pickSelectEntity.getProjectscale());
                pickResultArea.setPosition(position);
            }
        });
    }

    public void setMajorData(List<PickSelectEntity> data, PickResultEntity entity) {
        majorAdapter.setDatas(data);
        majorAdapter.setDefItem(entity.getPosition());
        if (pickResultMajor == null) {
            pickResultMajor = new PickResultEntity();
        }
        pickResultMajor.replace(entity);
    }

    public void setTeamSizeData(List<PickSelectEntity> data, PickResultEntity entity) {
        teamSizeAdapter.setDatas(data);
        teamSizeAdapter.setDefItem(entity.getPosition());
        if (pickResultTeamSize == null) {
            pickResultTeamSize = new PickResultEntity();
        }
        pickResultTeamSize.replace(entity);
    }

    public void setAreaData(List<PickSelectEntity> data, PickResultEntity entity) {
        areaAdapter.setDatas(data);
        areaAdapter.setDefItem(entity.getPosition());
        if (pickResultArea == null) {
            pickResultArea = new PickResultEntity();
        }
        pickResultArea.replace(entity);
    }

    public interface FilterResultClick {
        void result(PickResultEntity pickResultMajor, PickResultEntity pickResultTeamSize, PickResultEntity pickResultArea);

        void dimiss();
    }

    public void setFilterResultClick(FilterResultClick filterResultClick) {
        this.filterResultClick = filterResultClick;
    }

    @Override
    public void dismiss() {
        super.dismiss();
        if (filterResultClick != null) {
            filterResultClick.dimiss();
        }
    }
}

